package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.GetCodeResponse;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.CitizenCard;
import sz.szsmk.citizencard.mvp.MyCitizenCardMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.HttpApi;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;

/**
 * Created by chensf on 17/10/1.
 */

public class MyCitizenCardPresenter extends AppBasePresenter<MyCitizenCardMvpView> {

    @Inject
    NetworkDateSource mNetworkDateSource;

    @Inject
    public MyCitizenCardPresenter() {
        getComponent().inject(this);
    }


    public void gainMyCitizenCard() {

        checkViewAttached();
        HttpApi httpApi = mNetworkDateSource.getHttpApi();
//
        CommonRequest commonRequest = ObjectSignatureUtil.getCommonRequest();
        String json = new Gson().toJson(commonRequest);
        L.e(Constant.TAG, "获取市民卡：" + json);
        mNetworkDateSource.getMyCitizenCard(commonRequest, new Action1<GetCodeResponse>() {
            @Override
            public void call(GetCodeResponse getCodeResponse) {
                if (getCodeResponse.getCode().equals("0000")){
                    List<GetCodeResponse.DataBean> dataX = getCodeResponse.getDataX();
                    ArrayList<CitizenCard> citizenCards = new ArrayList<>();
                    for (int i = 0; i < dataX.size(); i++) {
                        GetCodeResponse.DataBean dataBean = dataX.get(i);
                        String toJson = new Gson().toJson(dataBean);
                        CitizenCard citizenCard = new Gson().fromJson(toJson, CitizenCard.class);
                        citizenCards.add(citizenCard);
                    }
                    getMvpView().gainMyCitizenCard(citizenCards);
                }else {

                }


            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
//        mNetworkDateSource
//        httpApi.getMyCitizenCard(commonRequest).compose(RxNetworkHelp.<List<CitizenCard>>handleResult()).subscribe(new RxSubscribe<List<CitizenCard>>(getActivity()) {
//            @Override
//
//            protected void onOk(List<CitizenCard> citizenCards) {
//                //List<CitizenCard> citizenCards = ccr.getCitizenCards();
//                getMvpView().gainMyCitizenCard(citizenCards);
//            }
//        });
    }
}

